/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: worker_thread.h
 *
 * Purpose: worker thead base API define and implmenetation
 *
 * Developer:
 *   wen.gu , 2023-06-20
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __UDSSIPC_WORKER_THREAD_H__
#define __UDSSIPC_WORKER_THREAD_H__

#include <functional>
#include <list>

#include "core/thread.h"


class JobBase {
public:
    using Runnable = std::function<void()>;
    using JobBasePtr = std::shared_ptr<JobBase>;
public:
    static constexpr uint32_t Job_Priority_Default = 0;
public:
    virtual ~JobBase() = default;
public:
    virtual void run() { }

};

class JobQueue {
public:
    using JobQueuePtr = std::shared_ptr<JobQueue>;
    using JobBaseList = std::list<JobBase::JobBasePtr>;
    using JobListArray = std::vector<JobBaseList>;
public:
    JobQueue(uint32_t max_size, uint32_t max_priority = JobBase::Job_Priority_Default);
    virtual ~JobQueue() = default;

public:
    virtual bool push(JobBase::JobBasePtr job_ptr, uint32_t job_priority = JobBase::Job_Priority_Default);
    virtual JobBase::JobBasePtr pop(uint32_t job_priority = JobBase::Job_Priority_Default);
    virtual JobBaseList popAll(uint32_t job_priority = JobBase::Job_Priority_Default);

protected:
    JobListArray job_list_array_;
};

class GeneralJobQueue: public JobQueue {
public:
    static constexpr uint32_t unlimited = 0; /** use this to constructure object indicate unlimt the queue size */
public:
    GeneralJobQueue(uint32_t size); /** limit the size of 'queue', if size=0, then unlimited */
    virtual ~GeneralJobQueue();

public:


};



class WorkerThread: public Thread {
public:
    using WorkerThreadPtr = std::shared_ptr<WorkerThread>;
public:

public:
    WorkerThread(const std::string& name);
    virtual ~WorkerThread();

public:
    virtual bool set_job_queue(JobQueue::JobQueuePtr jobq_ptr); //must set befre 'start' be called
    virtual JobQueue::JobQueuePtr job_queue();

    virtual bool doSync(JobBase::JobBasePtr job_ptr, uint32_t timeout_ms, uint32_t job_priority = JobBase::Job_Priority_Default);
    virtual bool doASync(JobBase::JobBasePtr job_ptr, uint32_t job_priority = JobBase::Job_Priority_Default);

    virtual bool doSync(JobBase::Runnable&& runnable, uint32_t timeout_ms, uint32_t job_priority = JobBase::Job_Priority_Default);
    virtual bool doASync(JobBase::Runnable&& runnable, uint32_t job_priority = JobBase::Job_Priority_Default);    

    virtual bool flush(uint32_t timeout_ms, uint32_t job_priority);

public:
    static WorkerThreadPtr Create(const std::string& name); 

protected:
    virtual void run();
private:
    class Impl;
    std::unique_ptr<Impl> impl_;
};





#endif /** !__UDSSIPC_WORKER_THREAD_H__ */